﻿using System;
using System.Collections.Generic;

public class EventArg : EventArgs
{
    public string nameArg;
    public object dataArg;

    public EventArg(string nameArg)
    {
        this.nameArg = nameArg;
    }

    public EventArg(string nameArg, object dataArg)
    {
        this.nameArg = nameArg;
        this.dataArg = dataArg;
    }
}

public class HandlerContainer
{
    public List<EventHandler<EventArg>> eventList;
    public HandlerContainer()
    {
        eventList = new List<EventHandler<EventArg>>();
    }
}

public class EventSub
{
    protected Dictionary<string, HandlerContainer> eventDic 
        = new Dictionary<string, HandlerContainer>();

    public void AddEvent(string eType, EventHandler<EventArg> eFunc)
    {
        if (!eventDic.ContainsKey(eType))
        {
            eventDic[eType] = new HandlerContainer();
        }
        var eventList = eventDic[eType].eventList;
        if (!eventList.Contains(eFunc))
        {
            eventList.Add(eFunc);
        }
    }

    public void RemoveEvent(string eType, EventHandler<EventArg> eFunc)
    {
        if (eventDic.ContainsKey(eType))
        {
            var eventList = eventDic[eType].eventList;
            if (eventList.Contains(eFunc))
            {
                eventList.Remove(eFunc);
            }
            if (eventList.Count <= 0)
            {
                eventDic.Remove(eType);
            }
        }
    }

    public void RemoveEvent(string eType)
    {
        if (eventDic.ContainsKey(eType))
        {
            eventDic.Remove(eType);
        }
    }

    public void BroadEvent(EventArg eventArg)
    {
        if (eventDic.ContainsKey(eventArg.nameArg))
        {
            var eventList = eventDic[eventArg.nameArg].eventList;
            for (int i = 0; i < eventList.Count; i++)
            {
                var eventBroad = eventList[i];
                if (eventBroad != null)
                {
                    eventBroad(this, eventArg);
                }
            }
        }
    }

    public void ClearEvent()
    {
        eventDic.Clear();
    }
}

public class EventCenter
{
    private static EventSub eventSub = new EventSub();

    public static void AddEvent(string eType, EventHandler<EventArg> eFunc)
    {
        eventSub.AddEvent(eType, eFunc);
    }

    public static void RemoveEvent(string eType, EventHandler<EventArg> eFunc)
    {
        eventSub.RemoveEvent(eType, eFunc);
    }

    public static void RemoveEvent(string eType)
    {
        eventSub.RemoveEvent(eType);
    }

    public static void UpdataEvent(bool isAddRem, string eType, EventHandler<EventArg> eFunc)
    {
        if (isAddRem)
        {
            AddEvent(eType, eFunc);
        }
        else
        {
            RemoveEvent(eType, eFunc);
        }
    }

    public static void BroadEvent(EventArg eventArg)
    {
        eventSub.BroadEvent(eventArg);
    }

    public static void ClearEvent()
    {
        eventSub.ClearEvent();
    }
}